package com.smartthinking.CICPNL.home.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.smartthinking.CICPNL.R;

public class Mapa extends android.support.v4.app.FragmentActivity {

	private GoogleMap mMap;
	private String Latitud, Longitud, title, snippet;
	private LatLng POI;
	public String URL;
	
	public void voidFinish(View V) {
		finish();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_mapa);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			Latitud = extras.getString("Latitud");
			Longitud = extras.getString("Longitud");
			title = extras.getString("title");
			snippet = extras.getString("snippet");
			Log.i("LAT", ""+Latitud);
			Log.i("LONG", ""+Longitud);
			Log.i("title", ""+title);
			Log.i("snippet", ""+snippet);
			
			if((isDouble(""+Latitud))&&(isDouble(""+Longitud))){
				POI = new LatLng(Double.parseDouble(Latitud), Double.parseDouble(Longitud));
			}else{
				POI = new LatLng(Double.parseDouble("19.432692"), Double.parseDouble("-99.133303"));
			}

		
			setUpMapIfNeeded();
		}
	}
	
	   boolean isDouble(String str) {
	        try {
	            Double.parseDouble(str);
	            return true;
	        } catch (NumberFormatException e) {
	            return false;
	        }
	    }

	private void setUpMapIfNeeded() {
		// Do a null check to confirm that we have not already instantiated the map.
		if (mMap == null) {
			mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

			// Check if we were successful in obtaining the map.
			if (mMap != null) {
				mMap.setMyLocationEnabled(true);
				Location loc = mMap.getMyLocation();

				Marker kiel = mMap.addMarker(new MarkerOptions()
				.title(""+title)
				.snippet(""+snippet)
            	.position(POI).icon(BitmapDescriptorFactory
						.fromResource(R.drawable.marker)));
				
            	mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(POI, 15));

            	
            	double[] location = getGPS();
//            	Log.i("LAT", ""+location[0]);
//            	Log.i("LNG", ""+location[1]);
            	URL = "http://maps.google.com/maps?saddr="+location[0]+","+location[1]+
    			"&daddr="+POI.latitude+","+POI.longitude+"";

            	
//            	URL = "https://maps.google.com/maps?saddr=19.367025,-99.155045&daddr=19.361033,-99.106636";
            	
//            	http://maps.google.com/maps?saddr=19.367025,-99.155045&addr=19.361033,-99.106636
            	Button bt_ruta = (Button) findViewById(R.id.bt_trazar);
            	bt_ruta.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent goNavegador = new Intent(Mapa.this, Navegador.class);
						goNavegador.putExtra("video_URL", ""+URL);
						startActivity(goNavegador);
					}
				});
            	
            	
            	
//            	Polyline line1 = mMap.addPolyline(new PolylineOptions()
//            	 .add(new LatLng(50.5, 34.1), new LatLng(40.7, -74.0))
//            	 .width(5)
//            	 .color(0xFFFF0000));

				//            	

			}
		}
	}
	
	
	private double[] getGPS() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> providers = lm.getProviders(true);

        /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
        Location l = null;
        
        for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null) break;
        }
        
        double[] gps = new double[2];
        if (l != null) {
                gps[0] = l.getLatitude();
                gps[1] = l.getLongitude();
        }
        return gps;
}

}
